import bson
from flask import jsonify, request

from app.libs.redprint import RedPrint
from app.db.models.push_report import ReportPush
from app.errors import ParameterError


api = RedPrint('push_report')


@api.route('/data/<string:pk>', methods=['GET'])
def get_report_data(pk):
    username = request.args.get("username")
    try:
        pk = bson.ObjectId(pk)
    except bson.errors.BSONError:
        raise ParameterError(msg="报告ID错误")
    if not (data := ReportPush._get_collection().find_one(
            {"_id": pk},
            {"_id": 0, "receiver_data": 0, "reporter_data": 0, "uid": 0, "push_time": 0}
    )):
        raise ParameterError(msg="查询的报告不存在")
    if data.get("username") != username:
        raise ParameterError(msg="用户信息错误")
    return jsonify({'code': 200, 'msg': 'ok', 'results': data})
